//
//  BTLeaderBoardVC.m
//  BaxTer
//
//  Created by 陈武琦 on 16/10/14.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTLeaderBoardVC.h"
#import "BTLeaderBoardCell.h"
#import "CWQ_tableView.h"
#import "BTLeaderBoardHeadV.h"
#import "masonry.h"
@interface BTLeaderBoardVC ()
@property(nonatomic, strong)NSArray *dataSource;
@property(nonatomic, strong)CWQ_tableView *tableView;
//上拉加载的页面
@property (nonatomic, assign) NSInteger page;
@end

@implementation BTLeaderBoardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"积分排行榜";
    self.view.backgroundColor = HexRGB_WSJ(236, 236, 236);
    CWQ_tableView *tableView = [[CWQ_tableView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight - 108) style:UITableViewStylePlain cellClass:@"BTLeaderBoardCell" vc:self upload:NO];
    [self.view addSubview:tableView];
    _dataSource = [NSMutableArray array];
    _page = 1;
    _tableView = tableView;
    [tableView cwq_requestDataWithDown:YES];
    [self setupTop];
   
}

- (void)setupTop {
    BTLeaderBoardHeadV *topView = [BTLeaderBoardHeadV leaderBoardHeadView];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];

}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.tableView setupSourceArray:self.dataSource];
}



- (void)cwq_tableView:(CWQ_tableView *)tableView requestDataWithDown:(BOOL)down {
    if (down) {
        self.page = 1;
    }else {
        self.page++;
    }
    
    tableView.loading = YES;
    weakSelf_define;
    [[BTNetworkManager sharedMg] POST:BTIntegralBoardListUrl parameters:nil success:^(id responseObject) {
        
        if (responseObject_success) {
            NSMutableArray *arr = [BTLeaderBoardM mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            //判断是上拉刷新？  还是下拉加载？
            if (!down) {
                NSMutableArray *muArr = [weakSelf.dataSource mutableCopy];
                
                [muArr addObjectsFromArray:arr];
                weakSelf.dataSource = muArr;
                if (weakSelf.page == [responseObject[@"totalPage"] integerValue]) {
                    tableView.footer.hidden = YES;
                }
                [tableView.footer endRefreshing];
            }else {
                
                if (weakSelf.page == [responseObject[@"totalPage"] integerValue]) {
                    tableView.footer.hidden = YES;
                }else {
                    tableView.footer.hidden = NO;
                }
                self.dataSource = arr;
                [tableView.header endRefreshing];
            }
            if (self.dataSource.count<=0) {
                tableView.loading = NO;
            }
        }
        
    } failure:^(NSString *err) {
        
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BTLeaderBoardM *model = self.dataSource[indexPath.row];
    BTLeaderBoardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BTLeaderBoardCellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    cell.num.text = [NSString stringWithFormat:@"%zd",indexPath.row+1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  60;
}


@end
